<?php
class Tools {

  static function strtolower($str) {
    if (function_exists('mb_strtolower'))
      return mb_strtolower($str, 'utf-8');
    return strtolower($str);
  }

  /* returns the current url without parameters you specify - $url = urlWithout("page,name,test"); */
  static public function urlWithout($elementsToRemove, $attatchLastAmp = true) {
    $originalURL = "";
    $originalQueryString = "";
    $amp = "";
    $newQueryString = "";
    $q = "";
    $elementsToRemoveArray = array();
    $queryStringItems = array();
    $originalURL = $_SERVER['SCRIPT_NAME'];
    $originalQueryString = $_SERVER['QUERY_STRING'];
    if (strlen($originalQueryString) > 0) {
      if (strlen($elementsToRemove) > 0) {
        $queryStringItems = split("&", $originalQueryString);
        $elementsToRemoveArray = split(",", $elementsToRemove);
        for ($q = 0; $q < sizeof($queryStringItems); $q++) {
          if (!in_array(Tools::getItemName($queryStringItems[$q]), $elementsToRemoveArray)) {
            $newQueryString = $newQueryString . $amp . $queryStringItems[$q];
            $amp = "&amp;";
          }
        }
        if (strlen($newQueryString) > 0) {
          return $originalURL . "?" . $newQueryString . ($attatchLastAmp ? "&amp;" : '');
        } else {
          return $originalURL . "?";
        }
      } else {
        return $originalURL . "?" . $originalQueryString;
      }
    } else {
      return $originalURL . "?";
    }
  }

  /* needed by urlWithout function */
  static function getItemName($queryItem) {
    $getName = array();
    if (strlen($queryItem) > 0) {
      $getName = split("=", $queryItem);
      return $getName[0];
    } else {
      return "";
    }
  }

  /**
   * Random password generator
   *
   * @param integer $length Desired length (optional)
   * @return string Password
   */
  static public function passwdGen($length = 8) {
    $str = 'abcdefghijkmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    for ($i = 0, $passwd = ''; $i < $length; $i++)
      $passwd .= Tools::substr($str, mt_rand(0, Tools::strlen($str) - 1), 1);
    return $passwd;
  }

  static function substr($str, $start, $length = false, $encoding = 'utf-8') {
    if (function_exists('mb_substr'))
      return mb_substr($str, $start, ($length === false ? Tools::strlen($str) : $length), $encoding);
    return substr($str, $start, $length);
  }

  static function strlen($str) {
    if (function_exists('mb_strlen'))
      return mb_strlen($str, 'utf-8');
    return strlen($str);
  }
}
?>
